package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.LoginMapper;
import com.heima.admin.service.LoginService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdLoginDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vos.AdLoginVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, AdUser> implements LoginService {
    /**
     * 登录
     *
     * @param dto
     * @return
     */

    @Override
    public ResponseResult login(AdLoginDto dto) {
        //参数校验
        if (dto == null || StringUtils.isBlank(dto.getName())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询用户是否存在
        LambdaQueryWrapper<AdUser> queryWrapper = Wrappers.<AdUser>lambdaQuery();
        queryWrapper.eq(AdUser::getName, dto.getName())
                .eq(AdUser::getStatus, 9);
        AdUser one = getOne(queryWrapper);
        if (one == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "账号或者密码错误");
        }
        //校验用户密码
        String salt = one.getSalt();
        String pwd = DigestUtils.md5DigestAsHex((dto.getPassword() + salt).getBytes());
        String dbpwd = one.getPassword();
        if (!pwd.equals(dbpwd)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "账号或者密码错误");
        }
        //生成token
        String token = AppJwtUtil.getToken(one.getId().longValue());
        //封装返回数据
        AdLoginVo adLoginVo = new AdLoginVo();
        BeanUtils.copyProperties(one, adLoginVo);

        Map map = new HashMap();
        map.put("token", token);
        map.put("user", adLoginVo);
        return ResponseResult.okResult(map);
    }


    /**
     * 注册
     *
     * @param adUser
     * @return
     */
    @Override
    public ResponseResult register(AdUser adUser) {
        //校验参数(必须要有账号密码)
        if (adUser == null || StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())) {
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断用户名是否已存在
        LambdaQueryWrapper<AdUser> wrapper = Wrappers.<AdUser>lambdaQuery();
        wrapper.eq(AdUser::getName, adUser.getName());
        AdUser one = getOne(wrapper);
        if (one != null) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        //对密码加密(md5+salt)
        String salt = String.valueOf(UUID.randomUUID());
        String pwd = DigestUtils.md5DigestAsHex((adUser.getPassword() + salt).getBytes());
        //封装数据
        adUser.setSalt(salt);
        adUser.setPassword(pwd);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(9);

        //新增用户
        save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
